<template>
  <div class="box">
    <el-card style="width: 800px; margin-top: 180px; height: 300px">
      <el-form
        ref="ruleForm"
        style="margin-top: 40px"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <!-- <el-form-item label="菜单路由" prop="path">
          <el-input v-model="ruleForm.path" disabled />
        </el-form-item> -->
        <el-form-item style="margin-top: 100px">
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
          >提交</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import { updateChild, updatePa } from './api/index'
import { getMenuObj, removeMenuObj } from '@/utils/auth' // get token from cookie

export default {
  data() {
    return {
      ruleForm: {
        name: '',
        path: '',
        id: ''
      },
      level: '',
      rules: {
        name: [
          { required: true, message: '请输入活菜单名称', trigger: 'blur' }
        ],
        path: [{ required: true, message: '请输入活菜单路由', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.level = getMenuObj().level
    console.log(getMenuObj())
    console.log(this.level)
    this.ruleForm = {
      name: getMenuObj().meta.title,
      path: getMenuObj().idPath,
      id: getMenuObj().id
    }
  },
  mounted() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.level === 1) {
            updatePa(this.ruleForm).then((res) => {
              console.log(res)
              if (res.code === 1) {
                this.$message.success('修改成功')
                this.$store.dispatch('tagsView/delView', this.$route)
                setTimeout(() => {
                  this.$router.push({
                    path: '/dashboard'
                  })
                  location.reload()
                }, 1000)
              } else {
                console.log(res)
              }
            })
          } else {
            updateChild(this.ruleForm).then((res) => {
              console.log(res)
              if (res.code === 1) {
                this.$message.success('修改成功')
                this.$store.dispatch('tagsView/delView', this.$route)
                setTimeout(() => {
                  this.$router.push({
                    path: '/dashboard'
                  })
                  location.reload()
                }, 1000)
              } else {
                console.log(res)
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      // removeMenuObj()
      // this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped>
.box {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>

